
package Database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;


public class edit_puzzle {
    public static ResultSet query_table()
    {
        ResultSet set = null;
        try {
            set=Connect.Connect.Con().createStatement().executeQuery("select top 200 PuzzleID, [Name], account, Puzzle.[type] from Admin, Puzzle where Admin.AdminID=Puzzle.AdminID order by PuzzleID desc");
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle.class.getName()).log(Level.SEVERE, null, ex);
        }
        return set;
    }
    // Get detail question by give a puId
    public static ResultSet contenr(int Puid)
    {
        ResultSet set = null;
        try {
            set = Connect.Connect.Con().createStatement().executeQuery("select * from Pu_Question where PuzzleID=" + Puid);
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle.class.getName()).log(Level.SEVERE, null, ex);
        }
        return set;
    }
    public static boolean delete(int id)
    {
        boolean kq = false;
        try {
            int re1=Connect.Connect.Con().createStatement().executeUpdate("delete from Pu_Question where PuzzleID=" + id);
            int re2=Connect.Connect.Con().createStatement().executeUpdate("delete from Puzzle where PuzzleID=" + id);
            if(re1!=0&&re2!=0)
            {
                kq=true;
            }
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle.class.getName()).log(Level.SEVERE, null, ex);
        }
         return kq;
    }
    public static int gettypeof_puzzle(int id)
    {
        int kq=0;
        try {
            PreparedStatement st=Connect.Connect.Con().prepareStatement("select [type] from Puzzle, Pu_Question where Puzzle.PuzzleID=Pu_Question.PuzzleID and QueID=?");
            st.setInt(1, id);
            ResultSet set=st.executeQuery();
            if(set.next())
            {
                kq=set.getInt(1);
            }
            set.close();
            Connect.Connect.Con().close();
        } catch (SQLException ex) {
            Logger.getLogger(edit_puzzle.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kq;
    }


}
